<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model common\models\ResourceClasses */
$this->title = '分类管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
<script src="https://cdn.bootcss.com/qs/6.5.1/qs.min.js"></script>
<script src="https://cdn.bootcss.com/axios/0.16.2/axios.min.js"></script>
<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<div id="app" class="resource-classes-view">
    <el-tree
            :data="classes"
            show-checkbox
            ref="tree"
            node-key="id"
            :default-expand-all="true"
            :default-checked-keys="classes_checked"
            :props="defaultProps">
    </el-tree>

    <div class="form-group" style="margin-top: 30px;">
        <button type="submit" class="btn btn-success s-btn" @click="saveCate">保 存</button>
    </div>
</div>
<script>
    Vue.prototype.$axios = axios;
    axios.interceptors.request.use(function (config) {
        if (config.method === 'post') {
            config.data = Qs.stringify(config.data);
        }
        return config;
    }, function (error) {
        return Promise.reject(error);
    });
    new Vue({
        el: '#app',
        data: {
            classes: [],
            classes_checked: [],
            defaultProps: {
                children: 'children',
                label: 'label'
            }
        },
        mounted() {
            this.getCate();
        },
        methods: {
            //获取分类
            getCate() {
                let _this = this;
                this.$axios({
                    method: 'GET',
                    url: 'branch-classes/cate-list',
                    dataType: "json",
                    responseType: 'json' // 默认的
                }).then(function (data) {
                    if (data.data.status == 200) {
                        res = data.data.data;
                        _this.classes = res.allCate;
                        _this.classes_checked = res.branchCate;
                    } else {
                        layer.msg(data.data.msg, {icon: 2, time: 500},)
                    }
                }).catch(function (err) {
                    console.log(err);
                })
            },
            //保存分类
            saveCate() {
                //获取选择的分类
                //console.log(this.$refs.tree.getCheckedKeys());//ChangeCate
                let id = this.$refs.tree.getCheckedKeys();
                this.$axios({
                    method: 'POST',
                    url: 'branch-classes/change-cate',
                    data: {
                        id: JSON.stringify(id),
                        _csrf_frontend: '<?= Yii::$app->request->csrfToken ?>'
                    },
                    responseType: 'json' // 默认的
                }).then(function (data) {
                    if (data.data.status == 200) {
                        layer.msg(data.data.msg, {icon: 1, time: 500},)
                    } else {
                        layer.msg(data.data.msg, {icon: 2, time: 500},)
                    }
                }).catch(function (err) {
                    console.log(err);
                })
            }
        }

    });
</script>
